<?php
/**
 * Created by PhpStorm.
 * User: patrick wu
 * Date: 2020/10/31
 * Time: 11:18
 */
namespace App\Http\Controllers\Backend;

use App\Service\Live\AlterService;
use App\Service\Live\OperateService;

/**
 * webscoket communication is json encoded
 * so you don't need json encode param
 *
 * Class GoodsController
 * @package App\Http\Controllers\Backend
 */
class GoodsController extends BaseController
{
    /**
     * administrator add goods event handle
     * @param $websocket
     * @param $data
     */
    public function addGoods($websocket, $data) : void
    {
        $service    =   new OperateService();
        $room_name  =   $websocket->getUserId();
        $result     =   $service->goodsAdd($room_name, $data['good_id']);

        if ($result !== false) {
            $new_info   =   $service->goodsInfoSort($room_name);
            /**
             * emit client event
             */
            $websocket->to($this->current_room)->emit('clientGoods', $new_info);
            $emit_msg   =   ['goods' => $new_info, 'recommend' => $service->getRecommend($room_name), 'msg' => '添加商品成功'];
            $websocket->toUserId($this->room_id)->emit('backendGoods', $emit_msg);
        }
    }

    /**
     * administrator delete goods event handle
     * @param $websocket
     * @param $data
     */
    public function delGoods($websocket, $data) : void
    {
        $service    =   new OperateService();
        /**
         * update redis set
         */
        $room_name  =   $websocket->getUserId();
        $result     =   $service->goodsDelete($room_name, $data['good_id']);

        if ($result !== false) {
            $new_info   =   $service->goodsInfoSort($room_name);
            /**
             * emit client event
             */
            $websocket->to($this->current_room)->emit('clientGoods', $new_info);
            echo 'Emit Del Goods ' . $this->current_room . '......' . PHP_EOL;
            $emit_msg   =   ['goods' => $new_info, 'recommend' => $service->getRecommend($room_name), 'msg' => '删除商品成功'];
            $websocket->toUserId($this->room_id)->emit('backendGoods', $emit_msg);
        }
    }

    /**
     * administrator price goods
     * @param $websocket
     * @param $data
     */
    public function priceGoods($websocket, $data) : void
    {
        $service    =   new OperateService();
        $room_name  =   $websocket->getUserId();
        $result     =   $service->goodsPrice($data['good_id'], $data['good_price']);

        if ($result !== false) {
            $new_info   =   $service->goodsInfoSort($room_name);
            /**
             * emit client event
             */
            $websocket->to($this->current_room)->emit('clientGoods', $new_info);
            echo 'Emit Price Goods ' . $this->current_room . '......' . PHP_EOL;
            $websocket->toUserId($this->room_id)->emit('backendPriceGoods', ['code' => 0, 'msg' => '修改成功']);
        }
    }

    /**
     * administrator move good
     * @param $websocket
     * @param $data
     */
    public function moveGoods($websocket, $data) : void
    {
        $service    =   new OperateService();
        $room_name  =   $websocket->getUserId();
        $result     =   $service->goodsMove($room_name, $data['good_id'], $data['move']);

        if ($result !== false) {
            $new_info   =   $service->goodsInfoSort($room_name);
            /**
             * emit client event
             */
            $websocket->to($this->current_room)->emit('clientGoods', $new_info);
            echo 'Emit Move Goods ' . $this->current_room . '......' . PHP_EOL;
            $emit_msg   =   ['goods' => $new_info, 'recommend' => $service->getRecommend($room_name), 'msg' => '移动商品成功'];
            $websocket->toUserId($this->room_id)->emit('backendGoods', $emit_msg);
        }
    }

    /**
     * administrator update goods event handle
     * @param $websocket
     * @param $data
     */
    public function upGoods($websocket, $data) : void
    {
        $old_good   =   $data['old_goods'] ?? '';
        $new_good   =   $data['new_goods'] ?? '';
        if (empty($new_good) || empty($old_good)) {
            return;
        }

        $room_name  =   $websocket->getUserId();
        $service    =   new OperateService();
        $result     =   $service->goodsUpdate($room_name, $old_good, $new_good);
        if (!$result) {
            return;
        }
        $new_info   =   $service->goodsInfoSort($room_name);

        /**
         * emit client event
         */
        $websocket->to($this->current_room)->emit('clientGoods', $new_info);
        $emit_msg   =   ['goods' => $new_info, 'recommend' => $service->getRecommend($room_name), 'msg' => '更新商品成功'];
        $websocket->toUserId($room_name)->emit('backendGoods', $emit_msg);
    }

    /**
     * administrator recommend goods event handle
     * @param $websocket
     * @param $data
     */
    public function setRecommend($websocket, $data) : void
    {
        $service    =   new OperateService();
        $good_id    =   $data['good_id'];

        $goods_info =   $service->goodsInfo([$good_id]);
        /**
         * set goods recommend
         */
        $room_name  =   $websocket->getUserId();
        $service->goodsRecommend($room_name, $good_id);

        /**
         * emit client event
         */
        $websocket->to($this->current_room)->emit('clientRecommend', $goods_info);
        $websocket->toUserId($room_name)->emit('backendRecommend', $good_id);
    }

    /**
     * administrator cancel recommend goods event handle
     * @param $websocket
     * @param $data
     */
    public function cancelRecommend($websocket, $data) : void
    {
        $service    =   new OperateService();

        /**
         * set goods recommend
         */
        $room_name  =   $websocket->getUserId();
        $service->goodsCancelRecommend($room_name);

        /**
         * emit client event
         */
        $websocket->to($this->current_room)->emit('clientRecommend', []);
        $websocket->toUserId($room_name)->emit('backendRecommend', '');
    }

    /**
     * administrator add coupons event handle
     * @param $websocket
     * @param $data
     */
    public function addCoupons($websocket, $data) : void
    {
        $service    =   new OperateService();
        /**
         * update redis set
         */
        $coupon_id  =   $data['coupon_id'];
        $room_name  =   $websocket->getUserId();
        $result     =   $service->couponsAdd($room_name, $coupon_id);

        if ($result !== false) {
            //get all coupons info from mysql
            $coupons_info =   $service->couponsInfo([$coupon_id]);

            $new_info     =   $service->couponsInfoSort($room_name);

            /**
             * emit client event
             */
            $websocket->to($this->current_room)->emit('clientCoupons', $coupons_info);
            $emit_msg     =   ['coupons' => $new_info, 'msg' => '添加优惠券成功'];
            $websocket->toUserId($room_name)->emit('backendCoupons', $emit_msg);
        }
    }

    /**
     * administrator delete coupons event handle
     * @param $websocket
     * @param $data
     */
    public function delCoupons($websocket, $data) : void
    {
        $service    =   new OperateService();
        /**
         * update redis set
         */
        $coupon_id  =   $data['coupon_id'];
        $room_name  =   $websocket->getUserId();
        $result     =   $service->couponsDelete($room_name, $coupon_id);

        if ($result !== false) {
            $new_info     =   $service->couponsInfoSort($room_name);

            /**
             * emit client event
             */
            $emit_msg     =   ['coupons' => $new_info, 'msg' => '删除优惠券成功'];
            $websocket->toUserId($room_name)->emit('backendCoupons', $emit_msg);
        }
    }

    /**
     * administrator update coupons event handle
     * @param $websocket
     * @param $data
     */
    public function upCoupons($websocket, $data) : void
    {
        $old_coupon =   $data['old_coupon'] ?? '';
        $new_coupon =   $data['new_coupon'] ?? '';
        if (empty($old_coupon) || empty($new_coupon)) {
            return;
        }
        $service    =   new OperateService();
        $room_name  =   $websocket->getUserId();
        $result     =   $service->couponsUpdate($room_name, $old_coupon, $new_coupon);
        if ($result !== false) {
            $new_info     =   $service->couponsInfoSort($room_name);

            /**
             * emit client event
             */
            $emit_msg     =   ['coupons' => $new_info, 'msg' => '更新优惠券成功'];
            $websocket->toUserId($room_name)->emit('backendCoupons', $emit_msg);
        }
    }

}
